/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package servlet;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Marco
 */
@WebServlet(name = "GeneralController", urlPatterns = {"/GeneralController"})
public class GeneralController extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        ServletContext sc = getServletContext();
        HttpSession session = request.getSession();
        //String paramGet = (String)request.getParameter("settore");
        String op = request.getParameter("submit");
        System.out.println("**********************" + op);
        if(op.equals("Login")){
            RequestDispatcher rdLogin = sc.getRequestDispatcher("/ServletLogin");
            rdLogin.forward(request, response);
        }
        else if(op.equals("Registrazione")){
            RequestDispatcher rdRegistrazione = sc.getRequestDispatcher("/ServletRegistrazione");
            rdRegistrazione.forward(request, response);    
        }
        else if(op.equals("Logout")){
            //System.out.println(request.getRequestURI());
            RequestDispatcher rdLogout = sc.getRequestDispatcher("/homepage.jsp");
            session.invalidate();
            rdLogout.forward(request, response); 
        }
        else if(op.equals("CambiaPwd")){
            RequestDispatcher rdCambiaPwd = sc.getRequestDispatcher("/ServletCambiaPwd");
            session.setAttribute("pwd", "pwd");
            rdCambiaPwd.forward(request, response); 
        }
        else if(op.equals("Profilo")){
            //System.out.println(request.getRequestURI());
            RequestDispatcher rdCambiaPwd = sc.getRequestDispatcher("/ServletDatiUtente");
            rdCambiaPwd.forward(request, response); 
        } else if (op.equals("inserisciEvento")){
            RequestDispatcher inserisciEvento = sc.getRequestDispatcher("/gest_evento.jsp?op=inserisciEvento");
            request.setAttribute("messageUpload", "upload");
            inserisciEvento.forward(request, response);
        } else if (op.equals("modificaEvento")){
            RequestDispatcher modificaEv = sc.getRequestDispatcher("/ServletEvento");
            request.setAttribute("Evento", "visualizzaEventi");
            request.setAttribute("operation", "modifica");
            modificaEv.forward(request, response);
        }else if (op.equals("prenotaEvento")){
            RequestDispatcher prenotaEvento = sc.getRequestDispatcher("/ServletEvento");
            //request.setAttribute("modificaEvento", "init");
            request.setAttribute("Evento", "visualizzaEventi");
            request.setAttribute("operation", "prenota");
            prenotaEvento.forward(request, response);
        } else if (op.equals("cancellaEvento")){
            RequestDispatcher cancellaEv = sc.getRequestDispatcher("/ServletEvento");
            request.setAttribute("cancellaEvento", "init");
            request.setAttribute("Evento", "visualizzaEventi");
            request.setAttribute("operation", "cancella");
            cancellaEv.forward(request, response);
        } else if (op.equals("prenotaEvento")){
            RequestDispatcher rdCambiaPwd = sc.getRequestDispatcher("/gest_evento.jsp?op=prenotaEvento");
            rdCambiaPwd.forward(request, response);
        } else if (op.equals("inserimentoEv")){
            RequestDispatcher inserimentoEv = sc.getRequestDispatcher("/ServletEvento");
            request.setAttribute("Evento", "inserisci");
            inserimentoEv.forward(request, response);
        } else if(op.equals("visualizzaSingoloEvento")){
            RequestDispatcher visualizzaSingoloEvento = sc.getRequestDispatcher("/ServletEvento");
            request.setAttribute("Evento", "visualizzaEvento");
            visualizzaSingoloEvento.forward(request, response);
        } else if(op.equals("modificaSingoloEvento")){
            RequestDispatcher modificaSingoloEvento = sc.getRequestDispatcher("/ServletEvento");
            request.setAttribute("Evento", "modificaSingoloEvento");  
            modificaSingoloEvento.forward(request, response);
        }else if(op.equals("prenotaSingoloEvento")){
            RequestDispatcher rdMappa = sc.getRequestDispatcher("/ServletCheckPosti");
            request.setAttribute("idevento",(String) request.getParameter("idevento"));
            rdMappa.forward(request, response);
        }else if(op.equals("prenotaSingoloEventoUser")){
            String id = (String)request.getParameter("id");        
            request.setAttribute("idevento",id);
            RequestDispatcher rdMappa = sc.getRequestDispatcher("/ServletCheckPosti");
            rdMappa.forward(request, response);        
        }else if(op.equals("updateEvento")){
            RequestDispatcher modificaSingoloEvento = sc.getRequestDispatcher("/ServletEvento");
            request.setAttribute("Evento", "updateEvento");
            modificaSingoloEvento.forward(request, response);
            System.out.println("titolo: " + request.getParameter("titoloev"));
        }else if(op.equals("cancellaSingoloEvento")){
            RequestDispatcher cancellaSingoloEvento = sc.getRequestDispatcher("/ServletEvento");
            request.setAttribute("Evento", "cancellaSingoloEvento");
            request.setAttribute("cancellaEvento", "init");
            cancellaSingoloEvento.forward(request, response);      
        }else if (op.equals("gestioneUtente")){
            RequestDispatcher permessiUtente = sc.getRequestDispatcher("/ServletGestUtenti");
            request.setAttribute("op", "permessiUtente");
            permessiUtente.forward(request, response);
        }else if(op.equals("permessiUtAdmin")){
            RequestDispatcher permessiUtente = sc.getRequestDispatcher("/ServletGestUtenti");
            request.setAttribute("op", "permessiUtente");
            request.setAttribute("mode", "admin");
            permessiUtente.forward(request, response);
        }else if(op.equals("permessiUtUtente")){
            RequestDispatcher permessiUtente = sc.getRequestDispatcher("/ServletGestUtenti");
            request.setAttribute("op", "permessiUtente");
            request.setAttribute("mode", "utente");
            permessiUtente.forward(request, response);
        }else if(op.equals("permessiUtVerifica")){
            RequestDispatcher permessiUtente = sc.getRequestDispatcher("/ServletGestUtenti");
            request.setAttribute("op", "permessiVerifica");
            request.setAttribute("mode", "verifica");
            permessiUtente.forward(request, response);
        }else if(op.equals("eliminaUt")){
            RequestDispatcher eliminaUtente = sc.getRequestDispatcher("/ServletGestUtenti");
            request.setAttribute("op", "permessiUtente");
            request.setAttribute("mode", "elimina");
            eliminaUtente.forward(request, response);
        }else if(op.equals("selezioneposto")){
            RequestDispatcher rd = sc.getRequestDispatcher("/ServletMappaPosti");
            request.setAttribute("settore", request.getParameter("settore"));
            request.setAttribute("idevento", request.getParameter("evento"));
            rd.forward(request, response);
        }else if(op.equals("prenotaPosti")){
            RequestDispatcher rd = sc.getRequestDispatcher("/ServletEvento");
            request.setAttribute("Evento", "prenotaEvento");
            rd.forward(request, response);
        }else if(op.equals("acquista")){
            RequestDispatcher rd = sc.getRequestDispatcher("/ServletEvento");
            request.setAttribute("Evento", "bigliettoevento");
            rd.forward(request, response);
        }else if(op.equals("inserisciCampo")){
            RequestDispatcher rd = sc.getRequestDispatcher("/gest_campo.jsp");
            request.setAttribute("campo", "inserisci");
            rd.forward(request, response);
        }else if(op.equals("inserimentoCampo")){
            RequestDispatcher rd = sc.getRequestDispatcher("/ServletCampo");
            request.setAttribute("campo", "inserisci");
            rd.forward(request, response);
        }else if(op.equals("modificaCampo")){
            RequestDispatcher rd = sc.getRequestDispatcher("/ServletCampo");
            request.setAttribute("campo", "tabellaCampi");
            rd.forward(request, response);
        }else if(op.equals("modificaSingoloCampo")){
            RequestDispatcher rd = sc.getRequestDispatcher("/gest_campo.jsp");
            request.setAttribute("campo", "modificaSingoloCampo");
            rd.forward(request, response);
        }else if(op.equals("updateCampo")){
            RequestDispatcher rd = sc.getRequestDispatcher("/ServletCampo");
            request.setAttribute("campo", "updateCampo");
            rd.forward(request, response); 
        }else if(op.equals("prenotaCampo")){
            RequestDispatcher rd = sc.getRequestDispatcher("/gest_campo.jsp");
            request.setAttribute("campo", "sceglisport");
            rd.forward(request, response);
        }else if(op.equals("avantiSport")){
            RequestDispatcher rd = sc.getRequestDispatcher("/ServletCampo");
            request.setAttribute("campo", "campiSport");
            rd.forward(request, response);
        }else if(op.equals("prenotaSingoloCampo")){
            RequestDispatcher rd = sc.getRequestDispatcher("/gest_campo.jsp");
            request.setAttribute("campo", "sceglidata");
            rd.forward(request, response);
        }else if(op.equals("prenotazioneCampo")){
            RequestDispatcher rd = sc.getRequestDispatcher("/ServletCampo");
            request.setAttribute("campo", "prenotazioneCampo");
            rd.forward(request, response); 
        }else if(op.equals("visualizzaElencoCampi")){            
            RequestDispatcher rd = sc.getRequestDispatcher("/ServletListaCampi");            
            request.setAttribute("sport", "sport");
            rd.forward(request, response); 
        }else if(op.equals("visualizzaSingoloCampo")){            
            RequestDispatcher rd = sc.getRequestDispatcher("/ServletListaCampi"); 
            request.setAttribute("idcampo", "idcampo");
            rd.forward(request, response); 
        }else if(op.equals("visualizzaNews")){
            RequestDispatcher rd = sc.getRequestDispatcher("/ServletNews");
            rd.forward(request, response);
        }else if(op.equals("right-content")){
            RequestDispatcher rd = sc.getRequestDispatcher("/ServletNews");
            rd.include(request, response);
        }else if(op.equals("singolanews")){
            RequestDispatcher rd = sc.getRequestDispatcher("/ServletNews");
            rd.forward(request, response);
        }else if(op.equals("portalecampi")){
            RequestDispatcher rd = sc.getRequestDispatcher("/ServletPortaleCampi");
            rd.forward(request, response);
        }else if(op.equals("showevent")){
            RequestDispatcher rd = sc.getRequestDispatcher("/ServletShowEvent");
            rd.forward(request, response);
        }else if(op.equals("convalida")){
            RequestDispatcher rd = sc.getRequestDispatcher("/ServletConvalidaUtente");
            rd.forward(request, response);
        }else if(op.equals("recuperapsw")){
            RequestDispatcher rd = sc.getRequestDispatcher("/ServletRecuperaPsw");
            rd.forward(request, response);
        }
        
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
